package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.BusinessStatement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.BusinessStatementMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.base.query.QueryObject;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Statement;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private BusinessStatementMapper businessStatementMapper;

    @Override
    public void save(Appointment appointment) {
        //补充没有的属性：创建时间+预约状态为预约中
        appointment.setCreateTime(new Date());
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);

        appointmentMapper.insert(appointment);
    }

    //分页+过滤
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        List<Appointment> appointments = appointmentMapper.selectForList(qo);
        return new TablePageInfo<>(appointments);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    /**
     * 只有预约中的预约单才能进行修改操作
     * @param appointment
     */
    public void update(Appointment appointment) {
        //注意：传递来的对象没有的属性是null的处理情况
        //注意：只有预约状态的的预约单才能进行修改,否则抛出异常
        Appointment editAppointment = appointmentMapper.selectByPrimaryKey(appointment.getId());
        if(!Appointment.STATUS_APPOINTMENT.equals(editAppointment.getStatus())){
            throw new BusinessException("您好！请先联系客服进行预约");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

    /**
     *  //只有在预约中的预约单状态才能进行到店操作,否则抛出异常
     * @param id
     */
    public void arrivaled(Long id) {
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("您好！请先与客服联系预约！");
        }
        //预约单状态修改为：到店状态.
        appointmentMapper.updateStatus(id,Appointment.STATUS_ARRIVAL);//根据预约单id修改状态
    }

    /**
     * 只有预约中状态的预约单，才能进行取消操作；否则抛异常
     * @param id
     */
    public void cancel(Long id) {
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("您好！请先跟客服进行预约");
        }
        appointmentMapper.updateStatus(id,Appointment.STATUS_CANCEL);
    }

    /**
     * 软删除：只修改isDel状态为1
     * @param id
     */
    public void updateIsDel(Long id) {
        appointmentMapper.updateIsDel(id);
    }

    /**
     * 根据预约单生成结算单
     * @param appointmentId
     * @return
     */
    public BusinessStatement generaterStatement(Long appointmentId) {
        //1、只有到店状态的预约单才能进行结算
        Appointment appointment = appointmentMapper.selectByPrimaryKey(appointmentId);
        if(!Appointment.STATUS_ARRIVAL.equals(appointment.getStatus())){
            throw new BusinessException("抱歉！必须到店才能结算哦！");
        }
        //2、查询出预约单，看是否有此预约单，如果有则需要创建结算单,并插入到结算单
        BusinessStatement businessStatement=createStatement(appointment);
        businessStatementMapper.insert(businessStatement);//才能返回id
        return businessStatement;
    }

    //修改结算单状态
    public void updateAppointStatus(Long appointmentId, Integer statusSettle) {
        appointmentMapper.updateStatus(appointmentId,statusSettle);
    }

    //创建结算单
    private BusinessStatement createStatement(Appointment appointment) {
        BusinessStatement businessStatement = new BusinessStatement();
        businessStatement.setCustomerName(appointment.getCustomerName());
        businessStatement.setCustomerPhone(appointment.getCustomerPhone());
        businessStatement.setActualArrivalTime(appointment.getActualArrivalTime());
        businessStatement.setLicensePlate(appointment.getLicensePlate());
        businessStatement.setCarSeries(appointment.getCarSeries());
        businessStatement.setServiceType(appointment.getServiceType().longValue());
        businessStatement.setAppointmentId(appointment.getId());
        businessStatement.setInfo(appointment.getInfo());
        //注意需要设置该订单状态为：消费中+创建结算单的时间
        businessStatement.setStatus(BusinessStatement.STATUS_CONSUME);
        businessStatement.setCreateTime(new Date());
        return businessStatement;
    }


}
